<?php

/**
* alltosun.com 网站配置控制器 index.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 吴松键 (wusj@alltosun.com) $
* $Date: 2014-8-11 下午4:28:00 $
* $Id$
*/

class Action
{
    private $per_page = 20;

    /**
     * 显示网站配置
     */
    public function index()
    {
        $page          = Request::Get('page_no', 1);
        $status        = Request::Get('status', 1);
        $is_advenced   = Request::getParam('is_advenced', 0);
        $search_filter = Request::getParam('search_filter', array());

        // 拼装搜索条件
        $filter = array();
        if (isset($search_filter['title']) && $search_filter['title']) {
            $filter['`title` LIKE '] = '%'.$search_filter['title'].'%';
        }
        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter['add_time >='] = $search_filter['start_date'].' 00:00:00';
        }
        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $filter['add_time <='] = $search_filter['end_date'].' 23:59:59';
        }
        $filter['status'] = $status;

        $setting_list = array();
        $setting_count = _model('setting')->getTotal($filter);
        if ($setting_count) {
            $pager = new Pager($this->per_page);
            if ($pager->generate($setting_count)) {
                Response::assign('pager', $pager);
            }

            $setting_list = _model('setting')->getList($filter, ' ORDER BY `add_time` DESC '.$pager->getLimit($page));
        }

        Response::assign('setting_list', $setting_list);
        Response::assign('status', $status);
        Response::assign('search_filter', $search_filter);
        Response::assign('is_advenced', $is_advenced);
        Response::display('admin/setting_list.html');
    }

    public function add()
    {
        Response::display('admin/add.html');
    }

    public function edit()
    {
        $id = Request::Get('id', 0);

        $info = _uri('setting', $id);

        Response::assign('info', $info);
        Response::assign('id', $id);
        Response::display('admin/add.html');
    }

    /**
     * 保存
     */
    public function save()
    {
        $info = Request::getParam('info', array());
        $id   = Request::getParam('id', 0);

//         foreach ($info as $k=>$v) {
//             $info[$k] = trim(AnFilter::filter_string($v));
//         }

        $setting_info = _uri('setting', array('field'=>$info['field']));

        if ($setting_info && !$id) {
            return '该字段值已经存在';
        }

        if ($id) {
            unset($info['field']);
            _model('setting')->update($id, $info);
        } else {
            _model('setting')->create($info);
        }
        Response::redirect(AnUrl('setting/admin'));
    }

    public function delete()
    {
        $id = Request::getParam('id');

        if (!$id) {
            return '请选择要删除的内容';
        }

        $ids = explode(',', trim($id, ','));
        foreach ($ids as $k=>$v) {
            if (!$v) {
                continue;
            }

            $setting_info = _uri('setting', (int)$v);
            if (!$setting_info) {
                continue;
            }

            if (1 == $setting_info['status']) {
                _model('setting')->update($v, array('status'=>0));
            } else {
                _model('setting')->delete($v);
            }
        }

        return 'ok';
    }

    public function change_status()
    {
        $id     = Request::getParam('id', 0);
        $status = Request::getParam('status', 1);
        if (!$id) {
            return '请选择要更改的内容';
        }

        $setting_info = _uri('setting', $id);
        if ($status == $setting_info['status']) {
            return '已经执行过此操作，不用重复提交';
        }

        if (1 == $status) {
            _model('setting')->update($id, array('status'=>1));
        }

        return 'ok';
    }
}
?>